// src/stores/auth.js
import { defineStore } from 'pinia'
import type { User } from './types'
export const useAuthStore = defineStore('auth', {
  state: () => ({
    token: localStorage.getItem('token') || null,
    user: localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : null,
  }),
  getters: {
    getToken():string |null{
      return this.token
    },
    getUser():User {
      return this.user
    },
    isLoggedIn():Boolean {
      return this.token !== null
    },
  },
  actions: {
    setToken(token:string) {
      this.token = token
      localStorage.setItem('token', token)
    },
    setUser(user: User | null) {
      this.user = user
      localStorage.setItem('user', JSON.stringify(user))
    },

    clear() {
      this.token = null
      this.user = null
      localStorage.removeItem('token')
      localStorage.removeItem('user')
    },
  },
})


